package com.fun_ordering.controller;

import com.fun_ordering.components.ApiResponse;
import com.fun_ordering.components.PageResult;
import com.fun_ordering.pojo.MaterialStock;
import com.fun_ordering.service.MaterialStockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 原材料表controller
 * @author XiaoPi
 *
 */
@RestController
@RequestMapping("/materialStock")
public class MaterialStockController {

	@Autowired
	private MaterialStockService materialStockService;
	/**
	 * 返回全部列表
	 * @return
	 */
	@RequestMapping("/lossMater")
	public List<MaterialStock> lossMater(){
		return materialStockService.lossMater();
	}


	/**
	 * 返回全部列表
	 * @return
	 */
	@RequestMapping("/findAll")
	public ApiResponse<List<MaterialStock>> findAll(){

		return ApiResponse.success(materialStockService.findAll());
	}


	/**
	 * 返回全部列表
	 * @return
	 */
	@RequestMapping("/findPage")
	public PageResult  findPage(int page,int rows){
		return materialStockService.findPage(page, rows);
	}

	/**
	 * 增加
	 * @param materialStock
	 * @return
	 */
	@RequestMapping("/add")
	public ApiResponse add(@RequestBody MaterialStock materialStock){
		try {
			materialStockService.add(materialStock);
		return ApiResponse.success(null);
		} catch (Exception e) {
			e.printStackTrace();
		return ApiResponse.failed(null);
		}
	}

	/**
	 * 修改
	 * @param materialStock
	 * @return
	 */
	@RequestMapping("/update")
	public ApiResponse update(@RequestBody MaterialStock materialStock){
		try {
			materialStockService.update(materialStock);
			return ApiResponse.success(null);
		} catch (Exception e) {
			e.printStackTrace();
			return ApiResponse.failed(null);
		}
	}

	/**
	 * 获取实体
	 * @param stockId
	 * @return
	 */
	@RequestMapping("/findOne")
	public MaterialStock findOne(Integer stockId){
		return materialStockService.findOne(stockId);
	}

	/**
	 * 批量删除
	 * @param stockIds
	 * @return
	 */
	@RequestMapping("/delete")
	public ApiResponse delete(Integer [] stockIds){
		try {
			materialStockService.delete(stockIds);
			return ApiResponse.success(null);
		} catch (Exception e) {
			e.printStackTrace();
			return ApiResponse.failed(null);
		}
	}

		/**
	 * 查询+分页
	 * @param materialStock
	 * @param page
	 * @param rows
	 * @return
	 */
	@RequestMapping("/pageSearch")
	public PageResult search(@RequestBody MaterialStock materialStock, int page, int rows  ){
		return materialStockService.findPage(materialStock, page, rows);
	}

}
